
import http from "@/utils/ajax/http";
import api from "@/api";
let token = localStorage.getItem("token");
const state = {
    token: token ? token : '',
    session: {},
}


const actions = {
    login({ commit }, data) {
        return new Promise((resolve, reject) => {
            http.post(api.token.login, data).then(res => {
                if (res.code == api.code.OK) {
                    commit('setSession', res.data.session);
                    commit('setToken', res.data.token);
                }
                resolve(res);
            }).catch(err => {
                reject(err);
            });
        })
    },
    tokenLogin(context, data) {
        return new Promise((resolve, reject) => {
            // 使用token 登录
            var token = context.state.token;
            http.post(api.token.tokenLogin, { token }).then(res => {
                if (res.code == api.code.OK) {
                    // 登录成功
                    context.commit('setSession', res.data.session);
                    context.commit('setToken', res.data.token);
                    resolve(true);
                } else {
                    resolve(false);
                }
            }).catch(reject);
        });
    },
    logout({ commit }) {
        return new Promise((resolve, reject) => {
            http.post(api.token.logout).then(res => {
                if (res.code == api.code.OK) {
                    commit('setSession', {});
                    commit('setToken', '');
                }
                resolve(res);
            }).catch(err => {
                reject(err);
            })
        });
    }
}

const mutations = {
    setSession(state, data) {

        state.session = data;
    },
    setToken(state, token) {
        state.token = token;
        window.localStorage.setItem("token", token);
    }
}


export default {
    namespaced: true,
    state,
    actions,
    mutations
}


